//
//  BaseViewController.m
//  BoWuGuan
//
//  Created by 梁尚嘉 on 2016/10/31.
//  Copyright © 2016年 KT. All rights reserved.
//

#import "BaseViewController.h"
#import "SMRequestStateView.h"


NSString *const SM_NOTIFACATION_NEED_CARTITEM_SHOW = @"SM_NOTIFACATION_NEED_CARTITEM_SHOW";
NSString *const SM_NOTIFACATION_NEED_CARTITEM_UPDATE = @"SM_NOTIFACATION_NEED_CARTITEM_UPDATE";
NSString *const SM_NOTIFACATION_NEED_CARTITEM_DISMISS = @"SM_NOTIFACATION_NEED_CARTITEM_DISMISS";
@interface BaseViewController () {
    SMRequestStateView *_requestView;
}
@property (nonatomic, strong) UIView *navigationBarShadowView;
@end

@implementation BaseViewController



- (void)viewDidLoad {

    @weakify(self);
    _requestView = [[SMRequestStateView alloc]init];
    [self.view addSubview:_requestView];
    [_requestView setOnClickView:^{
        @strongify(self);
        [self reloadDataFromNetWork];
    }];
    [_requestView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    _requestView.hidden = YES;

    
    [super viewDidLoad];

    self.view.backgroundColor = SM_backGroundColor;
    self.fd_prefersNavigationBarHidden = true;
    [self.view addSubview:self.navigationBar];
    [self.navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@(0));
        make.height.equalTo(@(64));
    }];

    if (self.navigationController.viewControllers.count > 1) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"ic_navibar_back_round_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    }

    [self.navigationBar.superview insertSubview:backgroundView belowSubview:self.navigationBar];
    backgroundView.backgroundColor = self.navigationColor;
//    [_navigationBar setValue:backgroundView forKey:@"backgroundView"];

    if (_isPresented) {
        [self setupLeftCancelBarButton];
    }

}

- (void)setNavigationBarHidden:(BOOL)navigationBarHidden
{
    _navigationBarHidden = navigationBarHidden;
    backgroundView.hidden = navigationBarHidden;
    [self.navigationBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(navigationBarHidden?0:64));
    }];
}

- (void)setNavigationShadowHidden:(BOOL)navigationShadowHidden
{
    _navigationShadowHidden = navigationShadowHidden;
    self.navigationBarShadowView.hidden = navigationShadowHidden;
}

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.navigationItem.title = title;
}

- (UIColor *)navigationColor
{
    if (self.navigationController.viewControllers.count > 1) {
        return SM_naviSubColor;
    }else {
        return SM_sangouColor;
    }
}

- (void)setNavigationColor:(UIColor *)navigationColor
{
    backgroundView.backgroundColor = navigationColor;
//    [_navigationBar setBackgroundImage:[UIImage imageWithColor:self.navigationColor] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsCompact];
}


- (UINavigationBar *)navigationBar
{
    if (!_navigationBar) {
        _navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_HEIGHT)];
        [_navigationBar pushNavigationItem: self.navigationItem animated:false];
        [_navigationBar setItems:@[self.navigationItem] animated:NO];
        NSMutableDictionary *textAttrs              = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName]   = SM_blackColor;
        textAttrs[NSFontAttributeName]              = SM_CFont(37);
        [_navigationBar setBarStyle:UIBarStyleBlack];
//        [_navigationBar setShadowImage:[UIImage imageWithColor:SM_naviShadowColor]];
        [_navigationBar setTitleTextAttributes:textAttrs];
        [_navigationBar setTintColor:SM_blackColor];
        backgroundView = [UIView new];
        backgroundView.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, 64);
        [_navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
        UIView *shadowView = [UIView new];
        shadowView.backgroundColor = SM_lightGrayColor;
        [_navigationBar addSubview:shadowView];
        _navigationBarShadowView = shadowView;
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(@0);
            make.height.equalTo(@.5);
        }];
    }
    return _navigationBar;
}

- (UINavigationItem *)navigationItem
{
    if (!_navigationItem) {
        _navigationItem = [[UINavigationItem alloc]init];
        [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName:SM_CFont(33)} forState:UIControlStateNormal];
    }
    return _navigationItem;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
//    _navigationBar.backgroundColor = self.navigationColor;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [OMAnalyzer pageAppearCollect:NSStringFromClass(self.class)];
    SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_DISMISS, nil);
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_DISMISS, nil);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [OMAnalyzer pageDisappearCollect:NSStringFromClass(self.class)];
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.view bringSubviewToFront:self.navigationBar];
}

- (void)reloadDataFromNetWork {}

- (void)startAnimating:(BOOL)boolean {
    boolean ? ({
        if (!_requestView.superview) {
            [self.view addSubview:_requestView];
        }
        [_requestView startAnimating:nil];
    }) : ({
        [_requestView stopAnimatingAtAll];
    });
}

- (void)showNetWorkError {
    [_requestView errorNetWorkNoticing];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    [Storage removeImageMemoryCache];
}

- (void)setupLeftCancelBarButton
{
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_cancel_medium"]
                                                                           style:UIBarButtonItemStylePlain
                                                                          target:self
                                                                          action:@selector(backAction)];
}

- (void)backAction
{
    if (self.navigationController.viewControllers.count > 1) {
        NaviPopAnimated(YES);
    }
    else {
        NaviDismiss(^{

        });
    }

}

- (void)dealloc
{
//    [KTSystemStorage removeImageMemoryCache];
    [self.task cancel];
    NSLog(@"%@__dealloc",[self class]);
}

@end
